package cn.basic.social.demo.controller;

import cn.basic.social.demo.service.SocialCallbackService;
import cn.basic.social.demo.vo.JsonResult;
import cn.basic.social.manager.config.SocialRequestFactory;
import cn.basic.social.manager.handler.utils.qywxjson.AesException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName SocialCallbackController
 * @Description 第三方服务回调接口api
 * @Author zlf
 * @Date 2023/9/8 10:48
 */
@Slf4j
@RestController
@RequestMapping("/social")
public class SocialCallbackController {

    @Autowired
    private SocialCallbackService socialCallbackService;

    @Autowired
    private SocialRequestFactory socialRequestFactory;

    /**
     * 用于二维码登录时传递值，登录的时候透传校验
     * 缓存到内存中，登录socialfilter的时候再校验这个值
     *
     * @param
     * @return
     * @author zlf
     * @date 17:22 2023/9/8
     */
    @GetMapping(value = "state")
    public JsonResult<String> state() throws Exception {
        String loginState = socialRequestFactory.getLoginState();
        return JsonResult.success(loginState);
    }

    /**
     * 企业微信数据回调接口
     * 3.1 支持Http Get请求验证URL有效性
     * 3.2 支持Http Post请求接收业务数据
     *
     * @param
     * @return
     * @author zlf
     * @date 14:03 2023/9/8
     */
    @RequestMapping(value = "dataCallback", method = {RequestMethod.GET, RequestMethod.POST}, consumes = MediaType.APPLICATION_XML_VALUE, produces = "text/plain")
    public Object wechatPostPush(@RequestParam("msg_signature") String msg_signature,
                                 @RequestParam("timestamp") Integer timestamp,
                                 @RequestParam("nonce") String nonce,
                                 @RequestParam(required = false) String echostr,
                                 @RequestBody(required = false) String body) throws AesException {
        log.info("企业微信回调参数: {},时间戳: {},随机数: {}, 加密的字符串: {}, 解析参数：{}", msg_signature, timestamp, nonce, echostr, body);
        if (ObjectUtils.isEmpty(body)) {
            return socialCallbackService.verificationUrl(msg_signature, timestamp, nonce, echostr);
        }
        socialCallbackService.getCallBackParameter(msg_signature, timestamp, nonce, body, "yunkekaichuang");
        return "success";
    }

    /**
     * 企业微信指令回调接口
     * 3.1 支持Http Get请求验证URL有效性
     * 3.2 支持Http Post请求接收业务数据
     *
     * @param
     * @return
     * @author zlf
     * @date 10:52 2023/9/8
     */
    @RequestMapping(value = "/qywx/commandCallback", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/plain")
    public Object getCallBack(HttpServletRequest request) throws IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        String body = IOUtils.toString(request.getInputStream());
        String jsonString = JSONObject.toJSONString(parameterMap);
        log.info("企业微信回调参数：{},  解析参数：{}", jsonString, body);
        if (ObjectUtils.isEmpty(body)) {
            return socialCallbackService.verificationUrl(request);
        }
        socialCallbackService.getCallBackParameter(request, body, "yunkekaichuang");
        return "success";
    }

    /**
     * 企业微信登录授权指令回调接口
     * 3.1 支持Http Get请求验证URL有效性
     * 3.2 支持Http Post请求接收业务数据
     *
     * @param
     * @return
     * @author zlf
     * @date 10:52 2023/9/8
     */
    @RequestMapping(value = "/qywx/login/commandCallback", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/plain")
    public Object getLoginCallBack(HttpServletRequest request) throws IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        String body = IOUtils.toString(request.getInputStream());
        String jsonString = JSONObject.toJSONString(parameterMap);
        log.info("企业微信回调参数：{},  解析参数：{}", jsonString, body);
        if (ObjectUtils.isEmpty(body)) {
            return socialCallbackService.verificationUrl(request);
        }
        socialCallbackService.getCallBackParameter(request, body, "login");
        return "success";
    }


}
